<template>
  <div class="personCenter">
    <el-tabs v-model="activeTab">
      <el-tab-pane label="个人资料修改" name="revisePerson">
        <revisePerson :form="form" @submitForm="submitForm" ref="revisePerson" />
      </el-tab-pane>
      <el-tab-pane label="密码修改" name="revisePsw">
        <revisePsw :form="form" @submitForm="submitForm" ref="revisePsw" />
      </el-tab-pane>
    </el-tabs>
    <div class="footer">
      <auth-button v-if="editModel!=='V'" size="" type="primary" :label="'保存'" :loading="loading"
        @click="save"></auth-button>
      <auth-button v-if="activeTab === 'revisePsw'" size="" type="primary" :label="'重置密码'" :loading="intLoading"
        @click="initPsw"></auth-button>
    </div>
  </div>
</template>
<script>
  import AuthButton from '@/components/auth/authButton'
  import revisePerson from './revisePerson'
  import revisePsw from './revisePsw'
  import { sysUser } from '@/api/platformApi'
  export default {
    name: 'personalCenter',
    components: {
      AuthButton,
      revisePerson,
      revisePsw
    },
    data () {
      return {
        activeTab: 'revisePerson',
        form: {},
        loading: false,
        editModel: 'E',
        intLoading: false
      }
    },
    created () {
      this.getData()
      this.getMsgType()
    },
    activated () {
      this.getMsgType()
    },
    computed: {
      api () {
        return sysUser
      }
    },
    methods: {
      handleClick (tab, event) {
      },
      // 触发子组件的保存事件,验证表单
      save () {
        this.$refs[this.activeTab].save()
      },
      getData () {
        sysUser.get(this.$store.state.user.userId).then(res => {
          if (res.msg === '执行成功') {
            this.form = res.data
          } else {
            this.error('获取数据失败')
          }
        })
      },
      submitForm () {
        this.loading = true
        this.api.update(this.form.id, this.form).then(res => {
          if (res.msg === '执行成功') {
            this.success(res.msg)
          } else {
            this.error(res.msg)
          }
          this.loading = false
        }).catch(err => {
          console.log(err)
          this.loading = false
        })
      },
      getMsgType () {
        const { Type } = this.$route.query
        if (!Type) return
        this.activeTab = Type
      },
      // 初始化密码
      initPsw () {
        this.$confirm('初始化密码, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.intLoading = true
          sysUser.initita({'id': this.$store.state.user.userId}).then(res => {
            if (res.code === 0) {
              this.success(res.data)
            }
            this.intLoading = false
          })
        }).catch(() => {
        })
      }
    }
  }
</script>
<style scoped>
  .personCenter {
    background: white;
    padding-bottom: 40px;
  }
  .personCenter .footer {
    width: 100%;
    text-align: center;
    margin-top: 30px;
  }
</style>
